﻿using ORM;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ORM
{
 

    public  class DbConfigCommon 
    {
       
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        /// <returns></returns>
        public string GetConnetion(ConfigModel m_Config)
        {
            string Conn = string.Empty;
            switch ((DbType)m_Config.DataBaseType)
            {
                case DbType.SqlServer:
                    if (m_Config.UserName=="")
                    {
                        Conn = string.Format("server={0};Integrated Security=True;database={1}",
                                                  m_Config.DataAddress, m_Config.DataBase);
                    }
                    else
                    {
                        Conn = string.Format("server={0};user id={1};password={2};persistsecurityinfo=True;database={3}",
                                                  m_Config.DataAddress, m_Config.UserName, m_Config.PassWord, m_Config.DataBase);
                    }
                   
                    break;
                case DbType.MySql:
                    Conn = string.Format("Server={0};Database={3};Uid={1};Pwd={2};",
                           m_Config.DataAddress, m_Config.UserName, m_Config.PassWord, m_Config.DataBase);
                    break;
                case DbType.Oracle:
                    Conn = string.Format(@"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={3})));User ID={1};Password={2};",
                           m_Config.DataAddress, m_Config.UserName, m_Config.PassWord, m_Config.DataBase);
                    break;
                case DbType.PostgreSQL:
                    Conn = string.Format("PORT=5432;DATABASE={3};HOST={0};PASSWORD={1};USER ID={2}",
                           m_Config.DataAddress, m_Config.UserName, m_Config.PassWord, m_Config.DataBase);
                    break;
                case DbType.Sqlite:
                   string GetCurrentProjectPath = Environment.CurrentDirectory.Replace(@"\bin\Debug", "");//获取具体路径
                    Conn = @"DataSource=" + GetCurrentProjectPath + @"\SqlSugar4xTest.sqlite";
                    //Conn = string.Format("Data Source={0};Version=3;Password={1};",m_Config.DataAddress, m_Config.PassWord);
                    break;
            }
            return Conn;
        }

        

        /// <summary>
        /// 获取Sqlsugar配置
        /// </summary>
        /// <returns></returns>
        public ConnectionConfig GetSqlSugarConfig(ConfigModel m_Config)
        {
            ConnectionConfig config = new ConnectionConfig();
            //连接字符串
            config.ConnectionString = GetConnetion(m_Config);
            //数据库类型
            config.DbType = (DbType)m_Config.DataBaseType;
            //自动释放链接
            config.IsAutoCloseConnection = true;
            //从特性读取主键
            config.InitKeyType = InitKeyType.Attribute;
            //自定义拉姆达
            config.ConfigureExternalServices = new ConfigureExternalServices
            {
                SqlFuncServices = GetLambda()
            };
            return config;
        }

      

        /// <summary>
        /// Lambda自定义解析
        /// </summary>
        /// <returns></returns>
        public  List<SqlFuncExternal> GetLambda()
        {
            //Lambda自定义解析
            var expMethods = new List<SqlFuncExternal>
                        {
                            new SqlFuncExternal()
                            {
                                UniqueMethodName = "ToDateFormat",
                                MethodValue = (expInfo, dbType, expContext) =>
                                {
                                    switch (dbType)
                                    {
                                        case DbType.SqlServer:
                                            return $"CONVERT (VARCHAR (10), {expInfo.Args[0].MemberName}, 121 )";

                                        case DbType.MySql:
                                            return $"DATE_FORMAT( {expInfo.Args[0].MemberName}, '%Y-%m-%d' ) ";

                                        case DbType.Sqlite:
                                            return $"date({expInfo.Args[0].MemberName})";

                                        case DbType.PostgreSQL:
                                        case DbType.Oracle:
                                            return $"to_date({expInfo.Args[0].MemberName},yyyy-MM-dd)";

                                        default:
                                            throw new Exception("未实现");
                                    }
                                }
                            },
                        };
            return expMethods;
        }

      
    }
}
